package middleware

import (
	"github.com/gin-contrib/cors"

	"github.com/gin-gonic/gin"
)

//	func Cors() gin.HandlerFunc {
//		return func(context *gin.Context) {
//			method := context.Request.Method
//			context.Header("Access-Control-Allow-Origin", "*")
//			context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
//			context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
//			context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
//			context.Header("Access-Control-Allow-Credentials", "true")
//			if method == "OPTIONS" {
//				context.AbortWithStatus(http.StatusNoContent)
//			}
//			defer func() {
//				if err := recover(); err != nil {
//					log.Infof("Panic info is: %v", err)
//				}
//			}()
//			context.Next()
//		}
//	}
func Cors() gin.HandlerFunc {
	return cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"PUT", "PATCH", "GET", "POST", "OPTIONS", "DELETE"},
		AllowHeaders:     []string{"Origin", "Authorization", "Content-Type", "Accept"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			//return origin == "https://github.com"
			return true
		},
		//MaxAge: maxAge * time.Hour,
	})
}
